/*
 * @copyright 2010 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of My Quick Rules.
 *
 * My Quick Rules is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * My Quick Rules is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with My Quick Rules.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiprion.myquickrules.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

/**
 * @author Amphiprion
 * 
 */
public class XMLUtil {
	public static void writeDocument(Document doc, File f) throws Exception {
		FileOutputStream fos = new FileOutputStream(f);
		writeChilds(doc, fos);
		fos.close();
	}

	private static void writeChilds(Node node, OutputStream fos) throws Exception {
		NodeList nodeList = node.getChildNodes();
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node n = nodeList.item(i);
			if (n instanceof Element) {
				Element el = (Element) n;
				String name = el.getNodeName();
				if (name != null) {
					fos.write("<".getBytes());
					fos.write(name.getBytes());
					NamedNodeMap attrs = el.getAttributes();
					for (int j = 0; j < attrs.getLength(); j++) {
						fos.write(" ".getBytes());
						Node attr = attrs.item(j);
						fos.write(attr.getNodeName().getBytes());
						fos.write("='".getBytes());
						fos.write(attr.getNodeValue().getBytes());
						fos.write("'".getBytes());

					}
					fos.write(">".getBytes());
					writeChilds(el, fos);
					fos.write("</".getBytes());
					fos.write(name.getBytes());
					fos.write(">".getBytes());
				}
			} else if (n instanceof Text) {
				fos.write(((Text) n).getNodeValue().getBytes());
			}
		}
	}

	public static void unzip(File zip, String root) throws Exception {
		// Open the ZipInputStream
		ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zip));

		// Loop through all the files and folders
		for (ZipEntry entry = inputStream.getNextEntry(); entry != null; entry = inputStream.getNextEntry()) {

			String innerFileName = root + File.separator + entry.getName();
			File innerFile = new File(innerFileName);
			if (innerFile.exists()) {
				innerFile.delete();
			}

			// Check if it is a folder
			if (entry.isDirectory()) {
				// Its a folder, create that folder
				innerFile.mkdirs();
			} else {
				// Create a file output stream
				FileOutputStream outputStream = new FileOutputStream(innerFileName);
				final int BUFFER = 2048;

				// Buffer the ouput to the file
				BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream, BUFFER);

				// Write the contents
				int count = 0;
				byte[] data = new byte[BUFFER];
				while ((count = inputStream.read(data, 0, BUFFER)) != -1) {
					bufferedOutputStream.write(data, 0, count);
				}

				// Flush and close the buffers
				bufferedOutputStream.flush();
				bufferedOutputStream.close();
			}

			// Close the current entry
			inputStream.closeEntry();
		}
		inputStream.close();
	}
}
